#!/bin/bash

echo "=========================================="
echo "测试 Completion Suggester 的 input 数组要求"
echo "=========================================="

ES_URL="http://117.72.38.248:19200"

# 创建测试索引
echo "1. 创建测试索引..."
curl -s -X DELETE "$ES_URL/test-input-requirement" > /dev/null

curl -s -X PUT "$ES_URL/test-input-requirement" -H "Content-Type: application/json" -d '{
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "analyzer": "ik_max_word"
      },
      "nameSuggest": {
        "type": "completion",
        "analyzer": "simple",
        "search_analyzer": "simple"
      }
    }
  }
}' > /dev/null

echo "✅ 测试索引创建完成"

# 插入测试数据
echo ""
echo "2. 插入测试数据..."

# 数据1：有 input 数组
curl -s -X POST "$ES_URL/test-input-requirement/_doc/1" -H "Content-Type: application/json" -d '{
  "name": "MacBook Pro",
  "nameSuggest": {
    "input": ["MacBook Pro", "MacBook", "Pro", "苹果笔记本"],
    "weight": 10
  }
}' > /dev/null

# 数据2：没有 input 数组，只有普通字段
curl -s -X POST "$ES_URL/test-input-requirement/_doc/2" -H "Content-Type: application/json" -d '{
  "name": "iPhone 15 Pro",
  "nameSuggest": {
    "weight": 10
  }
}' > /dev/null

# 数据3：空的 input 数组
curl -s -X POST "$ES_URL/test-input-requirement/_doc/3" -H "Content-Type: application/json" -d '{
  "name": "iPad Air",
  "nameSuggest": {
    "input": [],
    "weight": 10
  }
}' > /dev/null

echo "✅ 测试数据插入完成"

# 等待索引刷新
sleep 2

# 测试自动补全
echo ""
echo "3. 测试自动补全功能..."

echo ""
echo "🔍 测试1：有 input 数组的数据（MacBook Pro）"
echo "输入 'Mac' 应该能匹配到："
curl -s "$ES_URL/test-input-requirement/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "Mac",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq -r '.suggest.test_suggest[0].options[] | "  ✅ " + .text + " (来源: " + ._source.name + ")"'

echo ""
echo "🔍 测试2：没有 input 数组的数据（iPhone 15 Pro）"
echo "输入 'iPh' 应该匹配不到："
result=$(curl -s "$ES_URL/test-input-requirement/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "iPh",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq '.suggest.test_suggest[0].options | length')

if [ "$result" = "0" ]; then
    echo "  ❌ 没有匹配结果（因为没有 input 数组）"
else
    echo "  ✅ 匹配到结果"
fi

echo ""
echo "🔍 测试3：空的 input 数组（iPad Air）"
echo "输入 'iPad' 应该匹配不到："
result=$(curl -s "$ES_URL/test-input-requirement/_search" -H "Content-Type: application/json" -d '{
  "suggest": {
    "test_suggest": {
      "prefix": "iPad",
      "completion": {
        "field": "nameSuggest"
      }
    }
  }
}' | jq '.suggest.test_suggest[0].options | length')

if [ "$result" = "0" ]; then
    echo "  ❌ 没有匹配结果（因为 input 数组为空）"
else
    echo "  ✅ 匹配到结果"
fi

echo ""
echo "🔍 测试4：普通字段搜索（对比）"
echo "搜索 'iPhone' 应该能找到："
curl -s "$ES_URL/test-input-requirement/_search" -H "Content-Type: application/json" -d '{
  "query": {
    "match": {
      "name": "iPhone"
    }
  }
}' | jq -r '.hits.hits[] | "  ✅ " + ._source.name'

echo ""
echo "=========================================="
echo "测试结果总结"
echo "=========================================="
echo ""
echo "📋 结论："
echo "✅ 有 input 数组 → 自动补全正常工作"
echo "❌ 没有 input 数组 → 自动补全无法工作"
echo "❌ 空的 input 数组 → 自动补全无法工作"
echo "✅ 普通字段 → 普通搜索正常工作"
echo ""
echo "🎯 关键点："
echo "1. Completion Suggester 只使用 input 数组中的字符串"
echo "2. 普通字段内容不会参与自动补全"
echo "3. input 数组不能为空"
echo "4. 自动补全和普通搜索是两个不同的功能"
